<?php

namespace App\Containers\AppSection\Profile\UI\API\Controllers;

use App\Containers\AppSection\File\UI\API\Transformers\FileTransformer;
use App\Containers\AppSection\Profile\Actions\ChangeProfileAvatarAction;
use App\Containers\AppSection\Profile\Actions\CreateOuterProfileAction;
use App\Containers\AppSection\Profile\Actions\DeleteProfileAvatarAction;
use App\Containers\AppSection\Profile\Actions\GetAvatarAction;
use App\Containers\AppSection\Profile\Actions\GetOuterProfileAction;
use App\Containers\AppSection\Profile\Actions\GetOuterProfilesAction;
use App\Containers\AppSection\Profile\Actions\GetOwnProfilesAction;
use App\Containers\AppSection\Profile\Actions\GetProfileAction;
use App\Containers\AppSection\Profile\Actions\GetProfileAvatarAction;
use App\Containers\AppSection\Profile\Actions\GetUserProfilesAction;
use App\Containers\AppSection\Profile\Actions\UpdateOuterProfileAction;
use App\Containers\AppSection\Profile\UI\API\Requests\ChangeProfileAvatarRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\CreateOuterProfileRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\DeleteProfileAvatarRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\GetAvatarRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\GetOuterProfileRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\GetOuterProfilesRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\GetOwnProfilesRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\GetProfileAvatarRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\GetProfileRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\GetUserProfilesRequest;
use App\Containers\AppSection\Profile\UI\API\Requests\UpdateOuterProfileRequest;
use App\Containers\AppSection\Profile\UI\API\Transformers\ProfileTransformer;
use App\Ship\Parents\Controllers\ApiController;
use DB;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;

class Controller extends ApiController
{
    public function createOuterProfile(CreateOuterProfileRequest $request): JsonResponse
    {
        return DB::transaction(function () use ($request) {
            $profile = app(CreateOuterProfileAction::class)->run($request);
            return $this->created($this->transform($profile, ProfileTransformer::class));
        });
    }

    public function getProfile(GetProfileRequest $request): array
    {
        $profile = app(GetProfileAction::class)->run($request);

        return $this->transform($profile, ProfileTransformer::class);
    }

    public function getOwnProfiles(GetOwnProfilesRequest $request): array
    {
        $profiles = app(GetOwnProfilesAction::class)->run($request);

        return $this->transform($profiles, ProfileTransformer::class);
    }

    public function getUserProfiles($id, GetUserProfilesRequest $request): array
    {
        $profiles = app(GetUserProfilesAction::class)->run($id, $request);

        return $this->transform($profiles, ProfileTransformer::class);
    }

    public function changeAvatar(ChangeProfileAvatarRequest $request): JsonResponse
    {
        return DB::transaction(function () use ($request) {
            $avatar = app(ChangeProfileAvatarAction::class)->run($request);
            return $this->created($this->transform($avatar, FileTransformer::class));
        });
    }

    public function getAvatar(GetAvatarRequest $request): StreamedResponse
    {
        return app(GetAvatarAction::class)->run($request);
    }

    public function getProfileAvatar(GetProfileAvatarRequest $request): StreamedResponse
    {
        return app(GetProfileAvatarAction::class)->run($request);
    }

    public function deleteAvatar(DeleteProfileAvatarRequest $request): JsonResponse
    {
        DB::transaction(function () use ($request) {
            app(DeleteProfileAvatarAction::class)->run($request);
        });

        return $this->deleted();
    }

    public function GetOuterProfiles(GetOuterProfilesRequest $request): array
    {
        $profiles = app(GetOuterProfilesAction::class)->run($request);

        return $this->transform($profiles, ProfileTransformer::class);
    }

    public function getOuterProfile($id, GetOuterProfileRequest $request): array
    {
        $profile = app(GetOuterProfileAction::class)->run($id, $request);

        return $this->transform($profile, ProfileTransformer::class);
    }

    public function updateOuterProfile(UpdateOuterProfileRequest $request): JsonResponse
    {
        DB::transaction(function () use ($request) {
            app(UpdateOuterProfileAction::class)->run($request);
        });

        return $this->noContent();
    }
}
